﻿
@{
    ViewData["Title"] = "Index";
    Layout = null;
}

<<link href="~/libs/layui-v2.9.21/layui-v2.9.21/layui/css/layui.css" rel="stylesheet" />

@* 查询条件 *@
<form class="layui-form layui-row layui-col-space16">
    <div class="layui-col-sm3">
        <input type="text" name="A" id="roleName" placeholder="角色" class="layui-input">
    </div>

    <button type="button" class="layui-btn layui-bg-blue" id="searchButton">查询</button>
    <button type="button" class="layui-btn" onclick="location.reload()">重置</button>
    <button type="button" class="layui-btn layui-bg-purple" onclick="addInfor()">添加</button>
</form>


@* 显示数据表格 *@
<table class="layui-hide" id="mainData" lay-filter="mainData" style="margin-top: 10px;margin-left: 20px">
</table>

<script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="upd">修改</a>
    <a class="layui-btn layui-btn-orange layui-btn-xs" lay-event="delete">删除</a>
</script>

<script src="~/libs/layui-v2.9.21/layui-v2.9.21/layui/layui.js"></script>
<script src="~/libs/jquery/jquery.js"></script>

<script>


    layui.use('table', function () {
       var table = layui.table;
       var form = layui.form;
       var laydate = layui.laydate;

          function showData(){
             table.render({
                elem: '#mainData',
                url: '/Role/Display',
                page: true, // 开启分页
                limit: 10, // 每页默认显示的条数
                limits: [10, 20, 30], // 每页条数的选择项
                cols: [
                      [
                      { field: 'roleName', title: '角色' },
                      { field: 'roleState',  title: '是否启用',templet: function (d) {
                         return d.roleState===1 ? '是' : '否';
                      }
                      },
                      { title: '操作', fixed: 'right', width: 300, align: 'center', toolbar: '#barDemo' }
                      ]
                ],
                where: {
                    roleName: $("#roleName").val(), //获取输入框的单位代码
                    page:1,
                    limit:10
                },
                parseData: function (res) {
                console.log(res);
                    return {
                         code: res.code,
                         count: res.data.currentPage,
                         data: res.data.queryable
                     };
                },
             })
          }

           showData()

          //查询
          $("#searchButton").on("click", function() {
              console.log("查询");
              showData();
          });

         // 触发单元格事件
         //删除
          table.on('tool(mainData)', function(obj) {
             var data = obj.data;//获取当前行数据
             if (obj.event === 'delete') {
                 //在这里进行删除操作
             layer.confirm('确定要删除吗？', {
                  btn: ['确定', '取消'] //按钮
              },
                   function () {
                              $.ajax({
                                  url: '/Role/Delete',
                                  type: 'post',
                                  data: {
                                    id: data.id
                                 },
                                  success: function (res) {
                                      console.log(res)
                                      if (res==1){
                                          layer.msg(res.msg, { icon: 1, time: 1000 }, function () {
                                              location.reload(); //刷新页面
                                          });
                                      } else {
                                          layer.msg(res.msg, { icon: 2, time: 1000 });
                                      }
                                  }
                              });
                   }, function () {
                    layer.msg('已取消', { icon: 1, time: 1000 });
                   });
             }
             if(obj.event === 'upd'){
               //iframe层
                    layer.open({
                        type: 2,
                        title: '修改计量单位',
                        shadeClose: true,
                        shade: 0.5,
                        area: ['600px', '60%'],
                        content: '/Role/UpdateView?id='+ data.id, //iframe的src
                        end: function () {
                            // iframe层关闭后刷新页面
                            location.reload(); // 刷新页面
                        }
                    });
             }

         });
    });



    //添加
    function addInfor() {
         //iframe层
         layer.open({
             type: 2,
             title: '添加角色',
             shadeClose: true,
             shade: 0.5,
             area: ['600px', '60%'],
             content: '/Role/AddView', //iframe的src
             end: function () {
                 // iframe层关闭后刷新页面
                 location.reload(); // 刷新页面
             }
         });
    }




</script>


